package com.jion.testcenter.controller;

import com.alibaba.fastjson.JSON;
import com.jion.testcenter.Entity.ConfEntity;
import com.jion.testcenter.Entity.ResCode;
import com.jion.testcenter.service.ApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api")
public class ControllerRest {
    @Autowired
    ApiService apiService;

    //新增，编辑测试环境
    @RequestMapping(value= "/EditEnv", method = RequestMethod.POST)
    public Object addEnv(@RequestBody ConfEntity confEntity){
        ResCode code = new ResCode();
        int result = apiService.EditEnvService(confEntity);
        if (result >0)
            {
                code.setCode("000");
                code.setMsg("提交成功");
                return code;
            }
            else{
                code.setCode("提交失败");
                return code;
            }
    }
    //获取测试环境列表
    @RequestMapping(value = "/GetEnv")
    public List<ConfEntity> GetEnv(){
        return apiService.SelectEnvService();
    }
    @RequestMapping(value = "/DelectEnv")
    public Object DelectEnv(@RequestParam (required = true) String id){
        System.out.println(id);
        ResCode code = new ResCode();
        int result = apiService.DelectEnv(id);
        if (result >0)
        {
            code.setCode("000");
            code.setMsg("删除成功");
            return code;
        } else{
            code.setCode("删除失败");
            return code;
        }
    }
    @RequestMapping(value = "/GetOneEnv")
    public Object GetOneEnv(@RequestParam (required = true) String id){
        return apiService.SelectOneEnv(id);
    }

    /**************************环境配置****************************************/
    @RequestMapping(value= "/EditMysqlConf", method = RequestMethod.POST)
    public Object addMysqlConf(@RequestBody ConfEntity confEntity){
        ResCode code = new ResCode();
        int result = apiService.EidtMysqlConf(confEntity);
        if (result >0)
        {
            code.setMsg("提交成功");
            code.setCode("0");
            return code;
        }
        else{
            code.setCode("提交失败");
            return code;
        }
    }
    @RequestMapping(value = "/GetMysqlConf")
    public List<ConfEntity> GetMysqlConf(){
        return apiService.SelectMysqlConf();
    }

    @RequestMapping(value = "/DelectMysqlConf")
    public Object DelectMysqlConf(@RequestParam (required = true) String mysqlID){
        System.out.println(mysqlID);
        ResCode code = new ResCode();
        int result = apiService.DelectMysqlConf(mysqlID);
        if (result >0)
        {
            code.setCode("1000");
            code.setMsg("删除成功");
            return code;
        } else{
            code.setCode("删除失败");
            return code;
        }
    }
    @RequestMapping(value = "/GetOneMysqlConf")
    public Object GetOneMysqlConf(@RequestParam (required = true) String mysqlID){
        return apiService.SelectOneMysqlConf(mysqlID);
    }
    /*******************登陆************************/
    @RequestMapping(value = "/userLogin")
    public Object userLogin(@RequestBody ConfEntity confEntity){
        System.out.println("接口");
        ResCode code = new ResCode();
        return code;
    }
}
